import time

from selenium import webdriver
from datetime import datetime, date, timedelta
from faker import Faker
import random

from selenium.webdriver import ActionChains

fake = Faker("zh_CN")

option = webdriver.ChromeOptions()
wd = webdriver.Chrome()

wd.get("http://172.16.60.11/")
wd.maximize_window()
wd.implicitly_wait(10)

# 点击登录按钮
wd.find_element_by_xpath("//../span[text() = '用户中心登录']").click()

# 输入邮箱
wd.find_element_by_xpath("//input[@placeholder= '邮箱']").send_keys("user@forensix.cn")

# 输入密码
wd.find_element_by_xpath("//input[@placeholder = '密码']").send_keys("123456")

# 点击“授权并登录”按钮
wd.find_element_by_xpath("//span[text() = '授权并登录']").click()


# 找到“委托登记”tab
wd.find_element_by_xpath("//li[text() = '委托登记']").click()

# 填写“委托类型”信息
jg = fake.province() + fake.city() + "公安局"# 委托机构
ActionChains(wd).move_by_offset(0, 0).click()
# wd.find_element_by_xpath("//input[@placeholder = '填写委托机构']").send_keys(jg)
# wd.find_element_by_xpath("//label[text() = '机构类型']").click()

# 填写“送检人登记”
name = fake.name()
wd.find_element_by_xpath("//input[@placeholder = '请输入姓名']").send_keys(name)
# 在下拉框中选择
wd.find_element_by_xpath("//span[text() = '"+name+"']").click()
wd.find_element_by_xpath('//input[@placeholder = "请输入职务"]').send_keys("警官")
police_id = "12345678"
wd.find_element_by_xpath('//input[@placeholder = "请输入警官证号码"]').send_keys(police_id)
phone = fake.phone_number()
wd.find_element_by_xpath('//input[@placeholder = "请输入联系电话"]').send_keys(phone)
# 在下拉框中选择已有电话
wd.find_element_by_xpath("//span[text() = '"+phone+"']").click()
wd.find_element_by_xpath('//input[@placeholder = "请输入单位"]').send_keys(jg)
wd.find_element_by_xpath('//input[@placeholder = "请输入联系地址"]').send_keys(jg)
wd.find_element_by_xpath('//div/input[@name = "file"]').send_keys(r'C:\Users\JYJ\Desktop\图片\微信图片_20200601160942.jpg')
wd.find_element_by_xpath('//span[text()="下一步"]').click()

# 填写“委托登记”
curr_time = datetime.now()
no = datetime.strftime(curr_time, '%Y%m%d%H%M%S')
wd.find_element_by_xpath('//input[@placeholder = "执法办案系统编号"]').send_keys(no)
ele = wd.find_element_by_xpath('//input[@placeholder="选择案件类型"]')
time.sleep(2)
ele.click()

# print(i)

x = "//div[@x-placement='bottom-start']//li[@class = 'el-select-dropdown__item'][{i}]"

CaseType = wd.find_element_by_xpath(x.format(i = str(random.randint(0, 30))))

ActionChains(wd).move_to_element(CaseType).click(CaseType).perform()
CaseName = fake.name() + CaseType.text + "案"

wd.find_element_by_xpath('//input[@placeholder = "请输入案件名称"]').send_keys(CaseName)

# 选择案件性质
wd.find_element_by_xpath('//input[@placeholder = "选择案件性质"]').click()
y = '//div[@x-placement="bottom-start"]//li[@class="el-select-dropdown__item"][{z}]'
y1 = y.format(z = str(random.randint(0, 9)))
time.sleep(3)
ActionChains(wd).move_to_element(ele).click(ele).perform()


# 立案时间   当前时间的前五天
t = datetime.today() + timedelta(days = -5)
tmp = wd.find_element_by_xpath('//input[@placeholder = "请选择立案时间"]')
tmp.send_keys(t.strftime("%Y-%m-%d %H:%M:%S"))

# 调解时间  当前时间的前三天
t = datetime.today() + timedelta(days = -3)
wd.find_element_by_xpath('//input[@placeholder = "请选择调解时间"]').send_keys(t.strftime("%Y-%m-%d %H:%M:%S"))


# 发案时间   当前时间的前十天
t = datetime.today() + timedelta(days = -10)
wd.find_element_by_xpath('//input[@placeholder = "请选择发案时间"]').send_keys(t.strftime("%Y-%m-%d %H:%M:%S"))


# 受理时间   当前时间
t = datetime.today()
wd.find_element_by_xpath('//input[@placeholder = "请选择受理时间"]').send_keys(t.strftime("%Y-%m-%d %H:%M:%S"))


# 随意点击一个元素，将选择时间的弹框去掉
ActionChains(wd).move_by_offset(0, 0).click().perform()
# ele = wd.find_element_by_xpath("//div[@x-placement = 'top-start']//button[@class='el-button el-picker-panel__link-btn el-button--default el-button--mini is-plain']")
# ActionChains(wd).move_to_element(ele).click(ele).perform()


# 案情摘要
wd.find_element_by_xpath('//textarea[@placeholder = "请输入案情摘要"]').send_keys("案情摘要案情摘要")



# 委托项目
x = '//div[@class="el-form-item is-required el-form-item--small"]//div[@class="el-checkbox-group"]/label'
lens = wd.find_elements_by_xpath(x)

x = '//div[@class="el-form-item is-required el-form-item--small"]//div[@class="el-checkbox-group"]/label[{p}]'
x1 = x.format(p = str(random.randint(1, len(lens))))
wd.find_element_by_xpath(x1).click()


# 相关人员
# 嫌疑人
wd.find_element_by_xpath('//span[text()="嫌疑人"]').click()
# 涉案人
wd.find_element_by_xpath('//span[text()="涉案人"]').click()

xyname = fake.name()
wd.find_element_by_xpath('//input[@placeholder="请输入嫌疑人姓名"]').send_keys(xyname)
wd.find_element_by_xpath('//li/span[text()="'+xyname+'"]').click()

sname = fake.name()
wd.find_element_by_xpath('//input[@placeholder="请输入涉案人姓名"]').send_keys(sname)
wd.find_element_by_xpath('//li/span[text()="'+sname+'"]').click()


# 点击下一步按钮
wd.find_element_by_xpath('//div[@class="step step-one"][2]//button/span[text()="下一步"]').click()


# 委托备注
# wd.find_element_by_xpath('//div[@class="step step-three"]//input[@placeholder="请输入本次委托的其他说明，备注等信息"]').\
#    send_keys("我是委托备注我是委托备注我是委托备注我是委托备注我是委托备注我是委托备注")

wd.find_element_by_xpath('//div[@class="step step-two"]//span[text()="提交"]').click()
time.sleep(1)

# 查看案件
# wd.find_element_by_xpath('//span[text()="查看案件"]').click()

wd.close()